﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c)Ruben Sainz de la Maza
 
	@class: 	Rooms
	@vertion: 	0.1
	@date:		25/02/08
				
	@language: 	ActionScript 3.0 / cliente
	@author:	Ruben Sainz de la Maza
			
**/

try { var dummy = Rooms; } catch(e)
{

	/**
	 * constructor
	 */
	function Rooms()
	{
	//	trace("#Rooms# constructor");		
	}


//_________________________________________________________________________________ USERS

	/**
	 * ingresar un usuario en una sala
	 */
	Rooms.prototype.pushUser = function(p_null, p_strNick, p_strRoom)//:Object
	{
		trace("#Rooms# pushUser  " + p_strNick + " en " + p_strRoom);
		var _strNick	= p_strNick;						//nick
		var _strRoom	= p_strRoom;						//nombre de la sala
		var _objClient	= application.objUsers[_strNick];	//ref al cliente
		var _objRoom	= application.objRooms[_strRoom];	//ref a la sala
		
		if(_objClient){
			
		//	if(!_objRoom) this.addRoom(null, _strRoom, _strNick, 6, "Publico");			//si la sala no existe, crearla
		//	if(application.objRooms["lobby"][_strNick]) this.popUserLobby(null, _strNick);	//si esta en el lobby quitarlo
			
			if((application.objUsers[_strNick].room != _strRoom) || (_strRoom == "lobby")){	//si no esta en el room, ingresarlo
				application.objUsers[_strNick].room = _strRoom;								//guardar el nombre de la sala en el usuario
				application.objRooms[_strRoom][_strNick] = _objClient;						//agregar el usuario en la sala
				application.objRooms[_strRoom].users++;										//actualizar la cantidad de usuarios
				
				if(_strRoom == "lobby"){
					this.usersInRoom("lobby");  //actualizar en los clientes la cantidad de usuarios en el lobby
					/*
					for(var i in application.objUsers){
						if(application.objUsers[i].room == "lobby"){
							if(_objRoom){
								trace("refreshRoom");
								application.objUsers[i].call("clientCall", null, {func:"Lobby/refreshRoom", arguments:application.objRooms[_strRoom]}); //llamada al cliente
							}else{
								trace("addRoom");
								application.objUsers[i].call("clientCall", null, {func:"Lobby/addRoom",  arguments:application.objRooms[_strRoom]}); //llamada al cliente
							}
						}
					}
					*/
				}
				
				trace("#Rooms# pushUser  " + _strRoom + ":" + application.objRooms[_strRoom].users + " Nick:" + _strNick);
				return {func:"pushUser", room:_strRoom, success:true};
			} else {
				trace("#Rooms# pushUser - el usuario: " + _strNick + " ya se encontraba en: " + _strRoom);
				return {func:"pushUser", room:_strRoom, success:false};
			}

			
		} else {
			trace("#Rooms# pushUser - el usuario no existe: " + _strNick);
			return {func:"pushUser", room:_strRoom, success:false};
		}
	}
	
	
	/**
	 * quitar a un usuario de la sala en la que se encuentra
	 */
	Rooms.prototype.popUser = function(p_null, p_strNick)//:Object
	{
		var _strNick	= p_strNick;
		var _objClient	= application.objUsers[_strNick];
		var _strRoom	= application.objUsers[_strNick].room;
		
		if(_objClient){												//verificar que existe el usuario
			application.objRooms[_strRoom][_strNick] = null; 		//eliminarlo de la sala
			application.objRooms[_strRoom].users--;					//recalcular cant usuarios
			
			if(_strRoom == "lobby") this.usersInRoom("lobby");  	//actualizar en los clientes la cantidad de usuarios en el lobby
			
			trace("#Rooms# popUser " + _strRoom + ":" + application.objRooms[_strRoom].users + " Nick:" + _strNick);
			if((application.objRooms[_strRoom].users == 0) && (_strRoom != "lobby")) this.deleteRoom(null, _strRoom);
			
			return {func:"popUser", room:_strRoom, success:true};
			
		} else {
			trace("#Rooms# popUser - " + _strNick + " no existe");
			return {func:"popUser", room:_strRoom, success:false};			
		}
	}
	
	
//_________________________________________________________________________________ ROOMS

	/**
	 * crear una nueva sala
	 */
	Rooms.prototype.addRoom = function(p_null, p_strRoom, p_strNick, p_maxUsers, p_type)//:Object
	{
		var _strRoom	= p_strRoom;
		var _strNick	= p_strNick;
		
		if(!application.objRooms[_strRoom]){
			application.objRooms[_strRoom] = new Object();
			application.objRooms[_strRoom].users = 0;
			application.objRooms[_strRoom].maxUsers = p_maxUsers;
			application.objRooms[_strRoom].status = "En espera";
			application.objRooms[_strRoom].type = p_type;
			application.objRooms[_strRoom].colors = {NEGRO:false, ROJO:false, AMARILLO:false, AZUL:false, BLANCO:false, VERDE:false};
			application.objRooms[_strRoom].room = _strRoom;
			trace("#Rooms# addRoom - nueva sala creada:" + _strRoom);
			
		//	this.pushUser(null, _strNick, _strRoom);
			return {func:"addRoom", room:_strRoom, success:true, users:0, maxUsers:p_maxUsers, status:"En espera", type:p_type};
			
		} else {
			trace("#Rooms# addRoom - la sala " + _strRoom + " ya existe");
			return {func:"addRoom", room:_strRoom, success:false};
		}
	}
	
	
	/**
	 * eliminar una sala
	 */	
	Rooms.prototype.deleteRoom = function(p_null, p_strRoom)//:Object
	{
		var _strRoom	= p_strRoom;
		
		if(application.objRooms[_strRoom]){
			application.objRooms[_strRoom] = null;

			trace("#Rooms# deleteRoom - sala " + _strRoom + " destruida");
			return {func:"deleteRoom", room:_strRoom, success:true};
			
		} else {
			trace("#Rooms# deleteRoom - la sala " + _strRoom + " no existe");
			return {func:"deleteRoom", room:_strRoom, success:false};			
		}
	}



//_________________________________________________________________________________ AUXILIARES

	/**
	 * enviar a todos los usuarios la cantidad de jugadores en la sala que se encuentra
	 */	
	Rooms.prototype.usersInRoom = function(p_strRoom)//:void
	{
		var _strRoom = p_strRoom;	//nombre de la sala
		
		for(var i in application.objUsers){
			if(application.objUsers[i].room == _strRoom){
				application.objUsers[i].call("clientCall", null, {func:"Lobby/usersInRoom", arguments:application.objRooms["lobby"].users}); //llamada al cliente
			}
		}
	}

	
	/**
	* devolver todas las salas al usuario que lo esta pidiendo
	*/
	Rooms.prototype.getRooms = function(p_null, p_strNick)
	{
		var _strNick = p_strNick;
		if(application.objUsers[_strNick]) application.objUsers[_strNick].call("clientCall", null, {func:"Lobby/setRooms", arguments:application.objRooms}); //llamada al cliente
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * setColor
	 */
/*	Rooms.prototype.setColor = function(p_null, p_strNick, p_strColor)
	{
		if(application.objUsers[p_strNick]){								//verificar si existe el usuario
		
			var roomActual = application.objUsers[p_strNick].room;

			for(var i in application.objUsers){
				if(application.objUsers[i].room == roomActual)
					application.objUsers[i].call("Rooms/changeColor", false, p_strNick, p_strColor);
			}			
			
			trace("#Rooms# setColor:" + p_strNick + " Color: " + p_strColor);
		}
	}
	*/
	

}